<script setup>
import { updateInfo } from '@/api/customer/info';
import getSearchConfig from '../info/config/userDialogSearch.js'
import getContentConfig from '../info/config/userContent.js'
import to from '@/utils/to'
import { systemBaseUrl } from '@/api/config/base.js'

const props = defineProps({
  modelValue: {
    type: Boolean,
  },
})
const emits = defineEmits(['update:modelValue', 'saveSuccess'])

const { proxy } = getCurrentInstance()
const route = useRoute()
const pageContentRef = ref(null)
const pageSearchRef = ref(null)
const pageName = ''
const requestUrl = 'user/listAll'
const requestBaseUrl = systemBaseUrl

const tableHideItems = ref(['todo'])
const searchConfig = getSearchConfig()
const contentConfig = getContentConfig()

const contentConfigComputed = computed(() => {
  contentConfig.hideItems = tableHideItems
  return contentConfig
})

const tableSelected = ref([])
const tableListener = {
  selectionChange: (selected) => {
    tableSelected.value = selected
  },
}
const search = () => {
  pageSearchRef.value?.search()
}

const descConfig = ref({})

const headerButtons = []

const showPageSearch = ref(true)

const loading = ref(false)

const commitClick = async () => {
  if (tableSelected.value.length === 0) {
    proxy.$modal.msgWarning('请勾选销售信息')
    return
  }
  if (tableSelected.value.length >= 2) {
    proxy.$modal.msgWarning('只允许勾选一个销售')
    return
  }
  loading.value = true
  const selectItems = tableSelected.value.map((item) => item)
  emits('saveSuccess', selectItems)
  // const [err, res] = await to(
  //   authUserSelectAll({ roleId: roleId, userIds: uIds.toString() })
  // )
  // if (res?.code === 200) {
  //   proxy.$modal.notifySuccess(res.msg)
  //   handleValueChange(false)
  //   // search()
  //   emits('saveSuccess', res)
  // }
  loading.value = false
}

const daliogOpen = () => {
  search()
}

const handleCancel = () => {
  handleValueChange(false)
}

const handleValueChange = (value) => {
  console.log('handleValueChange:', value)
  emits('update:modelValue', value)
}
</script>
<template>
  <div class="authUserDialog">
    <el-dialog
      width="850px"
      title="选择用户"
      :model-value="modelValue"
      @update:modelValue="handleValueChange($event)"
      draggable
      destroy-on-close
    >
      <PageSearch
        v-show="showPageSearch"
        ref="pageSearchRef"
        :pageName="pageName"
        :searchConfig="searchConfig"
      ></PageSearch>
      <PageContent
        ref="pageContentRef"
        :pageName="pageName"
        :contentConfig="contentConfigComputed"
        :descConfig="descConfig"
        :showPageSearch="showPageSearch"
        :headerButtons="headerButtons"
        :showEdit="false"
        :showDelete="false"
        :requestUrl="requestUrl"
        :tableListener="tableListener"
        :requestBaseUrl="requestBaseUrl"
      >
        <template #statusSlot="{ backData }">
          <el-tag
            :type="backData.status == '0' ? 'success' : 'danger'"
            class="mx-1"
            effect="light"
          >
            {{ backData.status == '0' ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </PageContent>
      <template #footer>
        <el-button :loading="loading" @click="handleCancel"> 取消 </el-button>
        <el-button type="primary" @click="commitClick" :loading="loading">
          保存
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.authUserDialog {
  :deep(.el-pagination) {
    padding-top: 20px;
  }
  :deep(.el-dialog__body) {
    padding-bottom: 0px;
  }
}
</style>
